package net.linku.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @ClassName DateUtils
 * @Description: 日期工具类
 * @Author SilenceTian
 * @Date 2023/12/28
 * @Version V1.0
 **/
public class DateUtils {
    /**
     * 得到yyyyMMHHMM<br>
     * 例如：return 2007-05-03 08:03<br>
     */
    public static String getYYYYMMHHMM() {
        SimpleDateFormat df = getSimpleDateFormat("yyyy-MM-dd HH:mm"); // 小写的hh取得12小时，大写的HH取的是24小时
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 得到yyyyMM<br>
     * 例如：return 20070503<br>
     */
    public static String getYYYYMM() {
        SimpleDateFormat df = getSimpleDateFormat("yyyyMMdd"); // 小写的hh取得12小时，大写的HH取的是24小时
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 得到yyyy-MM<br>
     * 例如：return 2007-05<br>
     */
    public static String getyyyyMM() {
        SimpleDateFormat df = getSimpleDateFormat("yyyy-MM");
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 得到yyyy<br>
     * 例如：return 2007<br>
     */
    public static String getYYYY() {
        SimpleDateFormat df = getSimpleDateFormat("yyyy");
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 得到HHmm<br>
     * 例如：return 08:00<br>
     */
    public static String getHHmm() {
        SimpleDateFormat df = getSimpleDateFormat("HH:mm"); // 小写的hh取得12小时，大写的HH取的是24小时
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 得到HHmmss<br>
     * 例如：return 20070503<br>
     */
    public static String getHHmmss() {
        SimpleDateFormat df = getSimpleDateFormat("HHmmss"); // 小写的hh取得12小时，大写的HH取的是24小时
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 得到HHmmss<br>
     * 例如：return 20070503<br>
     */
    public static String getHHmmss_cn() {
        SimpleDateFormat df = getSimpleDateFormat("HH：mm：ss"); // 小写的hh取得12小时，大写的HH取的是24小时
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 得到hh<br>
     * 例如：15--(2007-11-02 15:03:50)<br>
     */
    public static String gethhDate() {
        SimpleDateFormat df = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String strTemp = df.format(date);
        return strTemp.substring(11, 13);
    }

    /**
     * 得到当前时间-分钟 mm<br>
     * 例如 03--(2007-11-02 15:03:50)<br>
     */
    public static String getmmDate() {
        SimpleDateFormat df = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String strTemp = df.format(date);
        return strTemp.substring(14, 16);
    }

    /**
     * "2006-01-12 16:30"
     *
     * @param sDate
     * @return 2006年1月12 星期四 16:30:20
     */
    public static String getFullDateWeekTime(String sDate) {
        try {
            String formater = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat format = getSimpleDateFormat(formater);
            Date date = format.parse(sDate);
            // format.applyPattern("yyyy年M月d日 E HH:mm:ss");
            format.applyPattern("yyyy-MM-dd日 E");
            return format.format(date);
        } catch (Exception ex) {
            System.out.println("TimeUtil  getFullDateWeekTime" + ex.getMessage());
            return "";
        }
    }

    /**
     * "2006-01-12 16:30"
     *
     * @return 2006年1月12 星期四 16:30:20
     */
    public static String getFullDateWeekTimeNow() {
        String sDate = getCurrentAllDate();
        try {
            String formater = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat format = getSimpleDateFormat(formater);
            Date date = format.parse(sDate);
            // format.applyPattern("yyyy年M月d日 E HH:mm:ss");
            format.applyPattern("yyyy-MM-dd E");
            return format.format(date);
        } catch (Exception ex) {
            System.out.println("TimeUtil  getFullDateWeekTime" + ex.getMessage());
            return "";
        }
    }

    /**
     * 得到当前时间-数字<br>
     * 20071102150350
     *
     * @return String
     */
    public static String getCurrentAllIntDate() {
        SimpleDateFormat df = getSimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        return df.format(date);
    }

    public static String getCurrentAllIntDateMs() {
        SimpleDateFormat df = getSimpleDateFormat("yyyyMMddHHmmssSSS");
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 得到当前时间-秒 ss<br>
     * 例如 50--(2007-11-02 15:03:50)<br>
     */
    public static String getssDate() {
        SimpleDateFormat df = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String strTemp = df.format(date);
        return strTemp.substring(17, 19);
    }

    /**
     * 功能：得到当前时间yyyy-MM-dd<br>
     * 例如 2007-11-02<br>
     *
     * @return String
     */
    public static String getCurrentShotDate() {
        SimpleDateFormat df = getSimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 功能：得到当前时间yyyy-MM-dd<br>
     * 例如 2007-11-02<br>
     *
     * @return String
     */
    public static String getCurrentShotTime() {
        SimpleDateFormat df = getSimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 功能：得到当前时间HH:mm<br>
     * 例如 13:00<br>
     *
     * @return String
     */
    public static String getCurrentShortTimeMinute() {
        SimpleDateFormat df = getSimpleDateFormat("HH:mm");
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 功能：得到30天前时间yyyy-MM-dd<br>
     * 例如 2007-11-02<br>
     *
     * @return String
     */
    public static String getThirtyDaysAgoDate() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -30);
        return sdf.format(cal.getTime());
    }

    /**
     * 功能：得到昨天时间yyyy-MM-dd<br>
     * 例如 2007-11-02<br>
     *
     * @return String
     */
    public static String getYesterdayDate() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return sdf.format(cal.getTime());
    }

    public static String getNextDate() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return sdf.format(cal.getTime());
    }

    /**
     * 功能：得到昨天时间yyyyMMdd<br>
     * 例如 20071102<br>
     *
     * @return String
     */
    public static String getYesDate() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return sdf.format(cal.getTime());
    }

    public static SimpleDateFormat getSimpleDateFormat(String str) {
        SimpleDateFormat sf = new SimpleDateFormat(str);
        sf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return sf;
    }


    /**
     * 得到yyyy-MM-dd HH:mm:ss<br>
     * 例如：return 2007-05:03 25:03:19<br>
     */
    public static String getCurrentAllDate() {
        SimpleDateFormat df = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 小写的hh取得12小时，大写的HH取的是24小时
        Date date = new Date();
        return df.format(date);
    }

    public static String getShortTime() {
        SimpleDateFormat df = getSimpleDateFormat("MM-dd HH:mm"); // 小写的hh取得12小时，大写的HH取的是24小时
        Date date = new Date();
        return df.format(date);
    }

    public static String getSftDate(String name) {
        SimpleDateFormat df = getSimpleDateFormat("MM月dd日 " + name);
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 得到yyyyMMdd<br>
     * 例如：return 20070503<br>
     */
    public static String getCurrentIntDate() {
        SimpleDateFormat df = getSimpleDateFormat("yyyyMMdd"); // 小写的hh取得12小时，大写的HH取的是24小时
        Date date = new Date();
        return df.format(date);
    }

    public static int dayForWeek() {//获取今天是周几
        Calendar c = Calendar.getInstance();
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    public static String getDateAfter(int day) {//获取过几天的时间
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        Calendar now = Calendar.getInstance();
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return sdf.format(now.getTime());
    }

    public static int getDateAfterWeek(int day) {//获取的时间周几
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        Calendar now = Calendar.getInstance();
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        int dayForWeek = 0;
        if (now.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = now.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    //根据格式获取时间字符串
    public static String getDateStrByFormat(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    //根据时间获取两个时间段相差的分钟数
    public static Long getDateDiffInMinutes(Date startDate, Date endDate) {
        long diffInMilliseconds = endDate.getTime() - startDate.getTime();
        return diffInMilliseconds / (60 * 1000);
    }

    //根据当前时间增加一个小时
    public static Date getDateAddOneHour(Long Minutes) {
        long time = new Date().getTime() + 1000 * 60 * Minutes;
        return new Date(time);
    }


    //获取当前时间的前X天时间
    public static String getBeforeDayByFormat(String format, int before) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -before);
        Date d = c.getTime();
        return sdf.format(d);
    }

    //计算时间差，以天数为单位。如：2018-08-08 和 2018-08-05 相差3天
    public static int getDistanceMonths(Date startTime, Date endTime) {
        int days = 0;
        long time1 = startTime.getTime();
        long time2 = endTime.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        days = (int) (diff / (24 * 60 * 60 * 1000));
        return days;
    }

    //计算时间差，以小时为单位。如：2018-08-08 和 2018-08-07 相差24h
    public static long getDistanceDays(Date startTime, Date endTime) {
        long hour = 0;
        long time1 = startTime.getTime();
        long time2 = endTime.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        hour = diff / (60 * 60 * 1000);
        return hour;
    }

    //计算时间差，以小时为单位。如：2018-08-08 和 2018-08-07 相差24h
    public static long getDistanceHours(Date startTime, Date endTime) {
        long hour = 0;
        long time1 = startTime.getTime();
        long time2 = endTime.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        hour = diff / (60 * 60 * 1000);
        return hour;
    }

    public static long getDistanceMinutes(Date startTime, Date endTime) throws ParseException {

        long minutes = 0;
        long time1 = startTime.getTime();
        long time2 = endTime.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        minutes = diff / (60 * 1000);
        return minutes;
    }

    public static long getDistanceSeconds(Date startTime, Date endTime) throws ParseException {

        long minutes = 0;
        long time1 = startTime.getTime();
        long time2 = endTime.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        minutes = diff / (1000);
        return minutes;
    }
}
